1
2
3
4 package org.apache.velocity.tools.generic.directive;
5
6 import java.io.IOException;
7
8 import org.apache.velocity.exception.MethodInvocationException;
9 import org.apache.velocity.exception.ParseErrorException;
10 import org.apache.velocity.exception.ResourceNotFoundException;
11 import org.ieee.shinobu.demo.velocity.AbstractVelocityTestCase;
12
13 /***
14 * @author skawai
15 */
16 public class IfnullTest extends AbstractVelocityTestCase {
17
18 public void testNull() throws ParseErrorException, MethodInvocationException, ResourceNotFoundException, IOException, Exception {
19 this.getEngine().setProperty("userdirective", Ifnull.class.getName());
20
21 this.setTemplate("" +
22 "#ifnull($null)\n" +
23 "null!\n" +
24 "#set($wasnull = true)\n" +
25 "#end\n" +
26 "$wasnull\n" +
27 "");
28
29 this.setExpected("" +
30 "null!\n" +
31 "true\n" +
32 "");
33
34 this.assertVelocity();
35 }
36
37 public void testNotNull() throws ParseErrorException, MethodInvocationException, ResourceNotFoundException, IOException, Exception {
38 this.getEngine().setProperty("userdirective", Ifnull.class.getName());
39
40 this.setTemplate("" +
41 "#set($null = 'null')" +
42 "#ifnull($null)\n" +
43 "null!\n" +
44 "#set($wasnull = true)\n" +
45 "#end\n" +
46 "$wasnull\n" +
47 "");
48
49 this.setExpected("" +
50 "$wasnull\n" +
51 "");
52
53 this.assertVelocity();
54 }
55
56 public void testNotBlock() throws MethodInvocationException, ResourceNotFoundException, IOException, Exception {
57 this.getEngine().setProperty("userdirective", Ifnull.class.getName());
58
59 this.setTemplate("" +
60 "#ifnull()\n" +
61 "Not a block!\n" +
62 "");
63
64 this.setExpected("" +
65 "");
66
67 try {
68 this.assertVelocity();
69 fail("not a block!");
70 } catch (ParseErrorException expeceted) {
71 }
72 }
73
74 }